package models

import (
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql" // import your used driver
	"regexp"
	"time"
)

/**
 *  留言信息结构体
 */
type MessagePad struct {
	M_id    int64  `orm:"column(m_id);pk"` //设置主键
	Hdline  string // Username=> username
	Text    string // PassWd=> pass_wd
	Name    string
	Emil    string
	Remark  string
	Cr_time string
	Status  int64
	Headpic string
	Userid  int64
}

/**
 * 增加文章
 */
func MessageAdd(hdline, text, name, emil, remark, headpic string, userid int64) (insertId int64, err error) {
	o := orm.NewOrm()
	timeNow := time.Now()
	nowTime := timeNow.Format("2006-01-02 15:04:05")
	messagePad := MessagePad{}
	messagePad.Hdline = hdline
	messagePad.Text = text
	messagePad.Name = name
	messagePad.Emil = emil
	messagePad.Remark = remark
	messagePad.Cr_time = nowTime
	messagePad.Userid = userid
	messagePad.Headpic = headpic
	//user := NoteBook{Username: name, Passwd: passwd, Sex: sex, Content: content}
	id, err := o.Insert(&messagePad)
	if err != nil {
		log := UitlLog()
		log.Error("connect db err" + err.Error())
		return 0, err
	}
	//fmt.Println(id)
	//return user
	return id, err
}

/**
 * 获取；评论列表
 */
func GetNoteMegList(offset, limit int, stu int64) ([]MessagePad, error) {
	var MessagePad []MessagePad
	if stu > 0 {
		_, err := MessagePadObj().Filter("Status", stu).Offset(offset).Limit(limit).OrderBy("-Cr_time").All(&MessagePad)
		if err != nil {
			log := UitlLog()
			log.Error("connect db err" + err.Error())
			return nil, err
		}
		return MessagePad, nil
	}
	_, err := MessagePadObj().Offset(offset).Limit(limit).OrderBy("-Cr_time").All(&MessagePad)
	if err != nil {
		log := UitlLog()
		log.Error("connect db err" + err.Error())
		return nil, err
	}

	return MessagePad, nil
}

/**
 * 获取：总数
 */
func GetMegCount(stu int64) int64 {
	o := orm.NewOrm()
	MessagePad := new(MessagePad)
	if stu > 0 {
		cnt, err := o.QueryTable(MessagePad).Filter("Status", stu).Count()
		if err != nil {
			log := UitlLog()
			log.Error("connect db err" + err.Error())
		}
		return cnt
	}
	cnt, err := o.QueryTable(MessagePad).Count()
	if err != nil {
		log := UitlLog()
		log.Error("connect db err" + err.Error())
	}
	return cnt
}

/**
 * msg_id查询一篇信息
 */
func GetMsgByMid(id int64) *MessagePad {
	o := orm.NewOrm()
	MessagePad := new(MessagePad)
	//var noteBook *NoteBook =  new(NoteBook)

	MessagePad.M_id = id

	err := o.Read(MessagePad)

	if err == orm.ErrNoRows {
		log := UitlLog()
		log.Error("connect db err" + err.Error())
	} else if err == orm.ErrMissPK {
		log := UitlLog()
		log.Error("connect db err" + err.Error())
	}
	return MessagePad
}

/**
 * 根据ID删除数据
 */
func DelMsg(id int64) (int64, error) {

	o := orm.NewOrm()
	MessagePad := new(MessagePad)
	MessagePad.M_id = id

	num, err := o.Delete(MessagePad)
	if err != nil {
		log := UitlLog()
		log.Error("connect db err" + err.Error())
		return 0, err
	}
	return num, nil
}

/**
* 根据ID修改状态
 */
func UpMsgStayus(num, id int64) error {
	o := orm.NewOrm()
	messagePad := new(MessagePad)
	messagePad.M_id = id
	//fmt.Println(noteBook.Id)
	if o.Read(messagePad) == nil {
		messagePad.Status = num

		//fmt.Println(noteBook.Username)
		if _, err := o.Update(messagePad, "Status"); err != nil {
			log := UitlLog()
			log.Error("connect db err" + err.Error())
			return err
		}
	}
	return nil

}

/**
* 根据ID修改数据
 */
func UpMsg(id int64, hdline, text, emil, remark string) error {
	o := orm.NewOrm()
	messagePad := new(MessagePad)
	messagePad.M_id = id
	timeNow := time.Now()
	nowTime := timeNow.Format("2006-01-02 15:04:05")
	//fmt.Println(noteBook.Id)
	if o.Read(messagePad) == nil {
		messagePad.Hdline = hdline
		messagePad.Text = text
		messagePad.Emil = emil
		messagePad.Remark = remark
		messagePad.Cr_time = nowTime
		//fmt.Println(noteBook.Username)
		if _, err := o.Update(messagePad, "Hdline", "Text", "Emil", "Remark", "Cr_time"); err != nil {
			log := UitlLog()
			log.Error("connect db err" + err.Error())
			return err
		}
	}
	return nil

}

/**
* 获取最新数据
 */
func GetNowadaysMessage(offset, limit, stu int) ([]MessagePad, int64, error) {
	var MessagePad []MessagePad
	timeNow := time.Now()
	nowTime := timeNow.Format("2006-01-02")
	qs := MessagePadObj()
	id, err := qs.Offset(offset).Limit(limit).Filter("Status", stu).Filter("Cr_time__icontains", nowTime).OrderBy("-Cr_time").All(&MessagePad)
	if err != nil {
		log := UitlLog()
		log.Error("connect db err" + err.Error())
		return nil, 0, err
	}
	num, err2 := qs.Filter("Cr_time__icontains", nowTime).Count()
	if err2 != nil {
		log := UitlLog()
		log.Error("connect db err" + err2.Error())
		return nil, 0, err2
	}
	if id == 0 {
		_, err1 := qs.Offset(offset).Limit(limit).Filter("Status", stu).OrderBy("-Cr_time").All(&MessagePad)
		if err1 != nil {
			log := UitlLog()
			log.Error("connect db err" + err.Error())
			return nil, 0, err1
		}
		return MessagePad, 4, nil
	}
	return MessagePad, num, nil
}

/**
* 获取：糊查询数封装
 */
func MessagePadlikeCond(keyword string) *orm.Condition {
	cond := orm.NewCondition()
	cond1 := cond.And("Hdline__icontains", keyword).Or("Name__icontains", keyword).Or("Text__icontains", keyword).Or("Remark__icontains", keyword)
	reg := regexp.MustCompile(`^[\d]$`)
	if reg.FindString(keyword) != "" {
		cond1.Or("Cr_time__icontains", keyword)
	}
	return cond1
}

/**
 *  模糊查询
 */
func FuzzyQry(value string, offset, limit int) ([]MessagePad, error) {
	var MessagePad []MessagePad
	cond1 := MessagePadlikeCond(value)
	qs := MessagePadObj()
	qs = qs.SetCond(cond1)
	// WHERE ... AND ... AND NOT ... OR ...
	_, err := qs.Offset(offset).Limit(limit).All(&MessagePad)
	if err != nil {
		log := UitlLog()
		log.Error("connect db err" + err.Error())
		return nil, err
	}
	return MessagePad, nil
}

/**
* 获取：糊查询数
 */
func FuzzyQsCount(value string) int64 {

	cond1 := MessagePadlikeCond(value)

	qs := MessagePadObj()
	qs = qs.SetCond(cond1)
	// WHERE ... AND ... AND NOT ... OR ...
	num, err2 := qs.Count()
	if err2 != nil {
		log := UitlLog()
		log.Error("connect db err" + err2.Error())
		return 0
	}
	return num
}

/**
 *  MessagePad 表实例化
 */
func MessagePadObj() orm.QuerySeter {
	return orm.NewOrm().QueryTable(new(MessagePad))
}
